package com.xyy.saas.payment.merchant.core.vo;

import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.vo.*;
import com.xyy.saas.payment.dao.model.UserInfo;
import com.xyy.saas.payment.util.ApolloUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import util.AESUtil;
import java.util.*;

@Getter
@Setter
@Slf4j
public class UserInfoVo {
    private Long id;
    private String accountId;
    /**
     * 注册手机号
     */
    private String telNo;
    /**
     * 图片
     */
    private Map<String, String> picInfo;
    /**
     * 法人
     */
    private List<ContactInfo> contactInfo;
    /**
     * 企业信息
     */
    private BasicInfoVo basicInfo;
    /**
     * 结算信息
     */
    private SettleAccountDto settleInfo;
    /**
     * 操作人/管理员/授权人
     */
    private OperatorInfo operatorInfo;
    /**
     * 股东
     */
    private ShareHolderInfoVo shareHolderInfo;
    /**
     * 受益人
     */
    private BeneficiaryInfoVo beneficiaryInfo;
    /**
     * 审核状态
     */
    private String auditStatus;
    /**
     * 备注
     */
    private String message;
    private Date ctime;
    private Date utime;

    /**
     * domain -> vo
     */
    public static UserInfoVo transfer(UserInfo source) throws Exception {
        if(source == null) {
            return null;
        }

        UserInfoVo target = new UserInfoVo();
        target.setId(source.getId());
        target.setAccountId(source.getAccountId());
        target.setTelNo(AESUtil.decrypt(source.getTelNo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
        target.setPicInfo(JSONUtils.parse2Map(AESUtil.decrypt(source.getPicInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), String.class, String.class));
        target.setContactInfo(JSONUtils.parse2List(AESUtil.decrypt(source.getContactInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), ContactInfo.class));
        target.setBasicInfo(JSONUtils.parse2Object(AESUtil.decrypt(source.getBasicInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), BasicInfoVo.class));
        target.setSettleInfo(JSONUtils.parse2Object(AESUtil.decrypt(source.getSettleInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), SettleAccountDto.class));

        if(StringUtils.isNotBlank(source.getOperatorInfo())) {
            target.setOperatorInfo(JSONUtils.parse2Object(AESUtil.decrypt(source.getOperatorInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), OperatorInfo.class));
        }

        if(StringUtils.isNotBlank(source.getShareHolderInfo())) {
            target.setShareHolderInfo(JSONUtils.parse2Object(AESUtil.decrypt(source.getShareHolderInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), ShareHolderInfoVo.class));
        }

        if(StringUtils.isNotBlank(source.getBeneficiaryInfo())) {
            target.setBeneficiaryInfo(JSONUtils.parse2Object(AESUtil.decrypt(source.getBeneficiaryInfo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)), BeneficiaryInfoVo.class));
        }

        target.setAuditStatus(source.getAuditStatus());
        target.setMessage(source.getMessage());
        target.setUtime(source.getUtime());
        target.setCtime(source.getCtime());
        return target;
    }

    /**
     * vo -> domain
     */
    public static UserInfo transfer(UserInfoVo source) throws PaymentException {
        if(source == null) {
            return null;
        }

        try {
            UserInfo target = new UserInfo();
            target.setId(source.getId());
            target.setAccountId(source.getAccountId());
            target.setTelNo(AESUtil.encrypt(source.getTelNo(), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setPicInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getPicInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setContactInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getContactInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setBasicInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getBasicInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setSettleInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getSettleInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setOperatorInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getOperatorInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setShareHolderInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getShareHolderInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setBeneficiaryInfo(AESUtil.encrypt(JSONUtils.toJSON(source.getBeneficiaryInfo()), ApolloUtil.getProperty(Constants.USER_INFO_KEY)));
            target.setAuditStatus(source.getAuditStatus());
            target.setMessage(source.getMessage());
            target.setUtime(source.getUtime());
            return target;
        }
        catch(Exception ex) {
            log.error(ex.getMessage() ,ex);
            throw new PaymentException(ResultCodeEnum.ENCRYPT_ERROR);
        }
    }
}
